package com.example.gailv

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.widget.Button
import android.widget.EditText
import java.math.RoundingMode
import java.text.DecimalFormat

class MainActivity : AppCompatActivity() {

    lateinit var tbp:EditText
    lateinit var jrsp:EditText
    lateinit var grsp:EditText
    lateinit var jrkp:EditText
    lateinit var jg:EditText
    lateinit var zrsp:EditText
    lateinit var dqtsp:EditText
    lateinit var jg2:EditText
    lateinit var js:Button
    lateinit var js2:Button
    lateinit var res:Button
    lateinit var ss:String
    lateinit var xj:String
    var slc:Float = 0.00f

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tbp = findViewById(R.id.tbp_tx)
        jrsp = findViewById(R.id.jrsp_tx)
        grsp = findViewById(R.id.grsp_tx)
        jrkp = findViewById(R.id.jrkp_tx)
        jg = findViewById(R.id.jg_tx)
        js = findViewById(R.id.js_btn)
        zrsp = findViewById(R.id.zrsp_tx)
        dqtsp = findViewById(R.id.dqtsp_tx)
        jg2 = findViewById(R.id.jg2_tx)
        js2 = findViewById(R.id.js2_btn)
        res = findViewById(R.id.res_btn)

        js.setOnClickListener {
            var tbp_tx = tbp.text.toString().toFloat()
            var jrsp_tx = jrsp.text.toString().toFloat()
            var grsp_tx = grsp.text.toString().toFloat()
            var jrkp_tx = jrkp.text.toString().toFloat()

            var jrsp_get = (jrsp_tx * 100).toInt()
            var grsp_get = (grsp_tx * 100).toInt()
            var slyz = (jrsp_get - grsp_get).toFloat()
            //slyz= (slyz*0.01).toFloat()
            if (slyz < 0) {
                slyz = slyz * -1
            }
            if (jrkp_tx > tbp_tx) {
                //多方计算
                if (slyz < 10 || slyz.toInt() == 0) {
                    slyz = slyz + 100
                    var glj = 100 - slyz
                    if (glj < 0) {
                        glj = glj * -1
                        var hy = 100 - glj
                        xj = glj.toString() + "% 上升"
                        ss = hy.toString() + "% 下降，"
                        jg.setText(ss + " " + xj)
                    } else {
                        slyz = slyz - 100
                        xj = glj.toString() + "% 上升"
                        ss = slyz.toString() + "% 下降，"
                        jg.setText(ss + " " + xj)
                    }
                    if (glj.toInt() == 0) {
                        jg.setText("0% 上升， 100% 下降")
                    }

                } else {
                    var glj = 100 - slyz
                    ss = glj.toString() + "% 上升，"
                    xj = slyz.toString() + "% 下降"
                    jg.setText(ss + " " + xj)
                }
            } else {
                //空方计算
                slyz = slyz + 100
                var glj = 100 - slyz
                glj = glj * -1
                var hy = 100 - glj
                xj = glj.toString() + "% 下降"
                ss = hy.toString() + "% 上升，"
                jg.setText(ss + " " + xj)
            }
        }

        js2.setOnClickListener {
            var jrkp_tx = jrkp.text.toString().toFloat()
            var tbp_tx = tbp.text.toString().toFloat()
            var jrsp_tx = jrsp.text.toString().toFloat()
            var grsp_tx = grsp.text.toString().toFloat()
            var zrsp_tx = zrsp.text.toString()
            var dqtsp_tx = dqtsp.text.toString()


            var sl1 = jrsp_tx - grsp_tx
            var sl2 = zrsp_tx.toFloat() - dqtsp_tx.toFloat()
            if (sl1 < 0 && sl2 < 0) {
                slc = (sl1 + (sl2))
            } else {
                slc = (sl1 - (sl2))
            }

            if (jrkp_tx > tbp_tx) {
                //多方
                if (slc < 0) {
                    var slci = (slc * (-100)).toInt()
                    var lslc = 100 - slci
                    jg2.setText(slci.toString() + "% 上升，" + lslc.toString() + "% 下降")
                } else {
                    var slci = (slc * (100)).toInt()
                    var lslc = 100 - slci
                    jg2.setText(slci.toString() + "% 上升，" + lslc.toString() + "% 下降")
                }
            } else {
                //空方
                if (slc < 0) {
                    var slci = (slc * (-100)).toInt()
                    var lslc = 100 - slci
                    jg2.setText(slci.toString() + "% 下降，" + lslc.toString() + "% 上升")
                } else {
                    var slci = (slc * (100)).toInt()
                    var lslc = 100 - slci
                    jg2.setText(slci.toString() + "% 下降，" + lslc.toString() + "% 上升")
                }
            }
        }
        //一周前加的功能
        res.setOnClickListener {
            tbp.setText("")
            jrsp.setText("")
            grsp.setText("")
            jrkp.setText("")
            zrsp.setText("")
            dqtsp.setText("")
        }
        //2022-04-06  重置功能
    }
}